package com.tg.sieve_data.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.google.gson.Gson;
import com.tg.sieve_data.common.ResponseResult;
import com.tg.sieve_data.entity.VisitRecord;
import com.tg.sieve_data.po.TgGroupExportPO;
import com.tg.sieve_data.po.TgGroupPO;
import com.tg.sieve_data.po.VisitRecordExportPO;
import com.tg.sieve_data.service.VisitRecordService;
import com.tg.sieve_data.vo.VisitRecordVO;
import org.joda.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/visitRecord")
public class VisitRecordController {
    @Autowired
    private VisitRecordService visitRecordService;


    @RequestMapping("/statistics")
    public Object statistics(VisitRecordVO visitRecordVO,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        return visitRecordService.statistics(visitRecordVO, pageNum, pageSize);
    }

    @GetMapping("exportData")
    public void exportData(VisitRecordVO visitRecordVO,
                           HttpServletResponse response) throws IOException {
        try {
            System.out.println("1=" + LocalDateTime.now());

            List<VisitRecordExportPO> exportData = visitRecordService.selectExportDatas(visitRecordVO);
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("访问记录_" + System.currentTimeMillis(), "UTF-8") + "_" + exportData.size() + "条";
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

            ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), VisitRecordExportPO.class).build();
            WriteSheet writeSheet = EasyExcel.writerSheet("访问记录").build();


            excelWriter.write(exportData, writeSheet);
            // 完成写入
            excelWriter.finish();
            System.out.println("结束时间=" + LocalDateTime.now());
        } catch (Exception e) {
            // ... existing error handling code ...
            if (!response.isCommitted()) {
                response.reset();
                response.setContentType("application/json");
                response.setCharacterEncoding("utf-8");
                Map<String, Object> result = ResponseResult.serverError("导出数据失败: " + e.getMessage());
                response.getWriter().println(new Gson().toJson(result));
            } else {
                throw new RuntimeException("导出数据失败", e);
            }
        }
    }

    @GetMapping("delete")
    public Object delete(@RequestParam Integer id) {
        visitRecordService.removeById(id);
        return ResponseResult.success("删除成功");
    }
}
